﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Songhay.OpenXml.Tests
{
    using Songhay.OpenXml;
    using Songhay.OpenXml.Extensions;

    /// <summary>
    /// Summary description for WordMLContentControlTest
    /// </summary>
    [TestClass]
    public class WordMLContentControlTest
    {
        [TestInitialize]
        public void ClearPreviousTestResults()
        {
            var directory = Directory.GetParent(TestContext.TestDir);

            directory.GetFiles()
                .OrderByDescending(f => f.LastAccessTime).Skip(1)
                .ToList().ForEach(f => f.Delete());

            directory.GetDirectories()
                .OrderByDescending(d => d.LastAccessTime).Skip(1)
                .ToList().ForEach(d => d.Delete(true));
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        [TestMethod]
        public void ShouldLoadTestDocument()
        {
            var uri = Test.TESTPROJECTFOLDER + this.GetType().Name + @"\AmazonProductImage.xml";
            var xd = XDocument.Load(uri);
            Assert.IsNotNull(xd, "The expected document is not here.");

            var parts = xd.Root
                .Elements(OpenXmlUtility.NamespaceXmlPackage + "part");
            Assert.IsTrue(parts.Count() > 0,
                "The expected pkg:part collection is not here.");

            var docPart = parts
                .Where(e => (string)e.Attribute(OpenXmlUtility.NamespaceXmlPackage + "name") ==
                    "/word/document.xml");
            var count = docPart.Count();
            Assert.IsTrue(count == 1,
                "The expected document pkg:part is not here.");

            var xmlData = docPart
                .Elements(OpenXmlUtility.NamespaceXmlPackage + "xmlData")
                .FirstOrDefault();
            Assert.IsNotNull(xmlData, "The expected pkg:xmlData is not here.");

            var document = xmlData
                .Element(SonghayW.document);
        }

        [TestMethod]
        public void ShouldConvertSdtToAcronym()
        {
            var uri = Test.TESTPROJECTFOLDER + this.GetType().Name + @"\Acronym.xml";
            var document = OpenXmlUtility.LoadFlatDocument(uri);
            var sdt = document
                .Element(SonghayW.body)
                .Element(SonghayW.sdt);

            Assert.IsNotNull(sdt, "The expected w:sdt is not here.");

            Assert.IsTrue(sdt.HasSdtTagName("rxAcronym"),
                "The expected w:tag is not here.");

            var acronymText = sdt.GetSdtDisplayValue();
            var acronymTitle = sdt.GetSdtPropertyValue(SonghayW.alias);

            var acronym = new XElement(SonghayXhtml.acronym,
                new XAttribute(SonghayXhtml.title_attr, acronymTitle),
                acronymText
            );

            TestContext.WriteLine(acronym.ToString());
        }

        [TestMethod]
        public void ShouldConvertSdtToAmazonProductImage()
        {
            var uri = Test.TESTPROJECTFOLDER + this.GetType().Name + @"\ProductImage.xml";
            var document = OpenXmlUtility.LoadFlatDocument(uri);
            var sdt = document
                .Element(SonghayW.body)
                .Element(SonghayW.sdt);

            Assert.IsNotNull(sdt, "The expected w:sdt is not here.");

            Assert.IsTrue(sdt.HasSdtTagName("rxAmazonProduct"),
                "The expected w:tag is not here.");

            var sdtDescendants = sdt
                .Element(SonghayW.sdtContent)
                .Descendants(SonghayW.sdt);

            Assert.IsTrue(sdtDescendants.Count() == 3,
                "The expected w:sdt descendant count is not here.");

            var productUri = "http://www.amazon.com/exec/obidos/ASIN/{0}/thekintespacec00A/";

            var src = ""; var style = "";

            sdtDescendants.ToList()
                .ForEach(
                    e =>
                    {
                        if(e.HasSdtTagName("rxAmazonProductAsin"))
                        {
                            var asin = e.GetSdtDisplayValue();
                            productUri = string.Format(productUri, asin);
                        }

                        if(e.HasSdtTagName("rxAmazonProductCss"))
                        {
                            style = e.GetSdtDisplayValue();
                        }

                        if(e.HasSdtTagName("rxAmazonProductImagePath"))
                        {
                            src = e.GetSdtDisplayValue();
                        }
                    });

            var a = new XElement(SonghayXhtml.a,
                new XAttribute(SonghayXhtml.href, productUri),
                new XAttribute(SonghayXhtml.title_attr, "Buy this product at Amazon.com!"),
                new XElement(SonghayXhtml.img,
                    new XAttribute(SonghayXhtml.alt, "Amazon.com product"),
                    new XAttribute(SonghayXhtml.src, src),
                    new XAttribute(SonghayXhtml.style_attr, style)
                )
            );

            TestContext.WriteLine(a.ToString());
        }

        [TestMethod]
        public void ShouldConvertSdtToWebImage()
        {
            var uri = Test.TESTPROJECTFOLDER + this.GetType().Name + @"\WebImage.xml";
            var document = OpenXmlUtility.LoadFlatDocument(uri);
            var sdt = document
                .Element(SonghayW.body)
                .Element(SonghayW.sdt);

            Assert.IsNotNull(sdt, "The expected w:sdt is not here.");

            Assert.IsTrue(sdt.HasSdtTagName("rxImage"),
                "The expected w:tag is not here.");

            var sdtDescendants = sdt
                .Element(SonghayW.sdtContent)
                .Descendants(SonghayW.sdt);

            Assert.IsTrue(sdtDescendants.Count() == 4,
                "The expected w:sdt descendant count is not here.");

            var alt = ""; var imageLinkUri = ""; var src = ""; var style = "";

            sdtDescendants.ToList()
                .ForEach(
                    e =>
                    {
                        if(e.HasSdtTagName("rxImagePath"))
                        {
                            src = e.GetSdtDisplayValue();
                        }

                        if(e.HasSdtTagName("rxImageAlt"))
                        {
                            alt = e.GetSdtDisplayValue();
                        }

                        if(e.HasSdtTagName("rxImageCss"))
                        {
                            style = e.GetSdtDisplayValue();
                        }

                        if(e.HasSdtTagName("rxImageLink"))
                        {
                            imageLinkUri = e.GetSdtDisplayValue();
                        }
                    });

            var img = new XElement(SonghayXhtml.img,
                    new XAttribute(SonghayXhtml.alt, alt),
                    new XAttribute(SonghayXhtml.src, src),
                    new XAttribute(SonghayXhtml.style_attr, style));

            TestContext.WriteLine(img.ToString());

            if(!string.IsNullOrEmpty(imageLinkUri))
            {
                var a = new XElement(SonghayXhtml.a,
                    new XAttribute(SonghayXhtml.href, imageLinkUri),
                    new XAttribute(SonghayXhtml.title_attr, alt), img);

                TestContext.WriteLine(a.ToString());
            }

        }
    }
}
